package com.object3D
{
	import away3d.containers.ObjectContainer3D;
	import away3d.entities.Mesh;
	import away3d.materials.TextureMaterial;
	import away3d.utils.Cast;
	
	import com.manager.CarMaterial;
	import com.utils.PowerLoader;
	import com.vo.PartVo;
	
	import flash.display.Bitmap;

	public class BaseWheel
	{
		protected static var wheelMaterial:TextureMaterial;//轮子素材
		
		protected var wheel3D:ObjectContainer3D;//3D视图容器
		protected var wheel:Mesh;
		protected var rim:Mesh;
		protected var _path:String;
		
		public function show(parent:ObjectContainer3D,
							 x:Number = 0,y:Number = 0,z:Number = 0,isLeft:Boolean = false,path:String = null):void{
			_path = path;
			initFace();
			parent.addChild(wheel3D);
			wheel3D.x = x;// / parent.scaleX;
			wheel3D.y = y;// / parent.scaleY;
			wheel3D.z = z;// / parent.scaleZ;
			if(isLeft)wheel3D.rotationY = 180;
		}

		private function initFace():void
		{
			if(wheel3D == null){
				wheel3D = new ObjectContainer3D();
				init();
			}
		}
		
		protected function init():void
		{
			initMaterial();
			loadModel();
		}
		
		protected function initMaterial():void
		{
			if(wheelMaterial == null){
				wheelMaterial = new TextureMaterial();
				wheelMaterial.specular = 0.5;
				PowerLoader.loadFile("models/wheel.png",loadPNG,wheelMaterial);
			}
		}
		
		protected function loadPNG(bmp:Bitmap,material:TextureMaterial):void
		{
			material.texture = Cast.bitmapTexture(bmp);
		}
		
		protected function loadModel():void
		{
			PowerLoader.loadFile("models/Wheel.xml",loadMesh,'wheel');
			PowerLoader.loadFile("models/Rim.xml",loadMesh,'rim');
		}
		
		protected function loadMesh(mesh:Mesh,name:Object):void
		{
			var subMesh:Mesh = mesh.getChildAt(0) as Mesh;
			if((name as String) == 'wheel'){
				if(wheel != null){
					wheel3D.removeChild(wheel);
				}
				wheel = subMesh.clone() as Mesh;
				wheel.material = wheelMaterial;
				wheel3D.addChild(wheel);
			}else if((name as String) == 'rim'){
				rim = subMesh.clone() as Mesh;
				rim.material = CarMaterial.rimsMaterial;
				wheel3D.addChild(rim);
			}
		}
		/**
		 * 替换轮毂素材
		 * @param pvo
		 */		
		public function replaceWheel(pvo:PartVo):void{
			PowerLoader.loadFile(pvo.meshPath,loadMesh,'wheel');
//			PowerLoader.loadFile(pvo.texturePath,loadPNG,wheelMaterial);
		}
		
//		public function replaceWheelMaterial(path:String):void
//		{
//			PowerLoader.loadFile(path,replacePNG,wheelMaterial);
//		}
//		
//		private function replacePNG(bmp:Bitmap,material:TextureMaterial):void
//		{
//			material.texture = Cast.bitmapTexture(bmp);
//			wheel.material = wheelMaterial;
//		}
		
		
	}
}